package com.mallotec.android.timenote.ui.common;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;


/**
 * @author reborn
 * @program TimeNote
 * @description
 * @create 2020-10-15 03:59
 **/
public abstract class BaseFragment <FragmentInstance, ViewBinding extends ViewDataBinding>
        extends Fragment {

    protected ViewBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onInit(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        binding.setLifecycleOwner(this);
        onInitData(savedInstanceState);
    }

    /**
     * 获取布局 id
     * @return 布局 id
     */
    protected abstract int getLayoutId();


    /**
     * 初始化
     *
     * @param savedInstanceState 保存的 Bundle
     */
    protected abstract void onInit(Bundle savedInstanceState);

    /**
     * 初始化 data
     *
     * @param savedInstanceState 保存的 Bundle
     */
    protected abstract void onInitData(Bundle savedInstanceState);

    public abstract FragmentInstance getInstance();
}
